﻿using Abp.Dependency;
using Abp.Domain.Uow;
using Abp.Events.Bus.Handlers;
using RMM.Core.Entity.Users;
using RMM.Core.UserService;
using RMM.IdGen;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RMM.Users.Events
{
    public class ResigerEventHandler : IEventHandler<ResigerEventData>, ITransientDependency
    {
        private readonly IUserService _userservice;
        private readonly IUnitOfWorkManager _unitOfWorkManager;
        private readonly UIDManager UIDManager;

        public ResigerEventHandler(IUserService userservice
            , IUnitOfWorkManager unitOfWorkManager
            , UIDManager uIDManager)
        {
            _userservice = userservice;
            _unitOfWorkManager = unitOfWorkManager;
            UIDManager = uIDManager;
        }

        public async void HandleEvent(ResigerEventData eventData)
        {
            var info = eventData.OAuthUserInfo;
            var u = new User();
            u.OpenId = eventData.OpenId;
            u.UnionID = info.unionid;
            u.Name = info.nickname;
            u.NickName = info.nickname;
            u.UserName = info.nickname;
            u.TenantId = 1;
            u.Sex = info.sex - 1;
            u.Status = 1;
            u.Address = info.province + info.city;
            u.UserHead = info.headimgurl;
            u.Id = UIDManager.CreateId();
            await _userservice.CreateAsync(u);
            await _unitOfWorkManager.Current.SaveChangesAsync();
        }
    }
}
